package com.pf.publish_subscribe.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.List;

@Configuration
public class PublishSubScribeConfig {

    public static final String CHANNEL = "java.news";


    /**
     * 配置订阅者信息
     *
     * @param connectionFactory
     * @param subscriberList    订阅者集合
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   List<MessageListenerAdapter> subscriberList) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //配置订阅者和通信的CHANNEL
        for (MessageListenerAdapter adapter : subscriberList) {
            container.addMessageListener(adapter, new PatternTopic(CHANNEL));
        }

        return container;

    }
}
